<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>greedykls.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>[model,Z]</span>=<span class=defun_name>greedykpca</span>(<span class=defun_in>X,y,options</span>)<br>
<span class=h1>%&nbsp;GREEDYKLS&nbsp;Greedy&nbsp;Regularized&nbsp;Kernel&nbsp;Least&nbsp;Squares.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;greedykls(X)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;greedykls(X,options)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;approximates&nbsp;input&nbsp;vectors&nbsp;X&nbsp;in&nbsp;the&nbsp;feature</span><br>
<span class=help>%&nbsp;&nbsp;space&nbsp;using&nbsp;GREEDYKPCA.&nbsp;Then&nbsp;the&nbsp;regularized&nbsp;least&nbsp;squares</span><br>
<span class=help>%&nbsp;&nbsp;are&nbsp;applied&nbsp;on&nbsp;the&nbsp;approximated&nbsp;data.&nbsp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;See&nbsp;help&nbsp;of&nbsp;KLS&nbsp;for&nbsp;more&nbsp;info&nbsp;about&nbsp;regularize&nbsp;least&nbsp;squares.</span><br>
<span class=help>%&nbsp;&nbsp;See&nbsp;help&nbsp;of&nbsp;GREEDYKPCA&nbsp;for&nbsp;more&nbsp;info&nbsp;on&nbsp;approximation&nbsp;of&nbsp;data</span><br>
<span class=help>%&nbsp;&nbsp;in&nbsp;the&nbsp;feature&nbsp;space.</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Input&nbsp;column&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;y&nbsp;[num_data&nbsp;x&nbsp;1]&nbsp;Output&nbsp;values.</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Control&nbsp;parameters:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.ker&nbsp;[string]&nbsp;Kernel&nbsp;identifier.&nbsp;See&nbsp;HELP&nbsp;KERNEL&nbsp;for&nbsp;more&nbsp;info.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.arg&nbsp;[1&nbsp;x&nbsp;narg]&nbsp;Kernel&nbsp;argument.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.m&nbsp;[1x1]&nbsp;Maximal&nbsp;number&nbsp;of&nbsp;base&nbsp;vectors&nbsp;(Default&nbsp;m=0.25*num_data).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.p&nbsp;[1x1]&nbsp;Depth&nbsp;of&nbsp;search&nbsp;for&nbsp;the&nbsp;best&nbsp;basis&nbsp;vector&nbsp;(Default&nbsp;p=m).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.mserr&nbsp;[1x1]&nbsp;Desired&nbsp;mean&nbsp;squared&nbsp;reconstruction&nbsp;errors&nbsp;of&nbsp;approximation.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.maxerr&nbsp;[1x1]&nbsp;Desired&nbsp;maximal&nbsp;reconstruction&nbsp;error&nbsp;of&nbsp;approximation.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;See&nbsp;'help&nbsp;greedyappx'&nbsp;for&nbsp;more&nbsp;info&nbsp;about&nbsp;the&nbsp;stopping&nbsp;conditions.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.verb&nbsp;[1x1]&nbsp;If&nbsp;1&nbsp;then&nbsp;some&nbsp;info&nbsp;is&nbsp;displayed&nbsp;(default&nbsp;0).</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Kernel&nbsp;projection:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[nsv&nbsp;x&nbsp;new_dim]&nbsp;Multipliers&nbsp;defining&nbsp;kernel&nbsp;projection.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Selected&nbsp;subset&nbsp;of&nbsp;the&nbsp;training&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.nsv&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;basis&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.kercnt&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;kernel&nbsp;evaluations.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.MaxErr&nbsp;[1&nbsp;x&nbsp;nsv]&nbsp;Maximal&nbsp;reconstruction&nbsp;error&nbsp;for&nbsp;corresponding</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number&nbsp;of&nbsp;base&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.MsErr&nbsp;[1&nbsp;x&nbsp;nsv]&nbsp;Mean&nbsp;square&nbsp;reconstruction&nbsp;error&nbsp;for&nbsp;corresponding</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number&nbsp;of&nbsp;base&nbsp;vectors.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;x&nbsp;=&nbsp;[0:0.05:2*pi];&nbsp;y&nbsp;=&nbsp;sin(x)&nbsp;+&nbsp;0.1*randn(size(x));</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;greedykls(x,y(:),struct('ker','rbf','arg',1,'lambda',0.001));</span><br>
<span class=help>%&nbsp;&nbsp;y_est&nbsp;=&nbsp;kernelproj(x,model);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;hold&nbsp;on;</span><br>
<span class=help>%&nbsp;&nbsp;plot(x,y,'+k');&nbsp;plot(x,y_est,'b');&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;plot(x,sin(x),'r');&nbsp;plot(x(model.sv.inx),y(model.sv.inx),'ob');</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;KERNELPROJ,&nbsp;KPCA,&nbsp;GREEDYKPCA.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2005,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;01-mar-2005,&nbsp;VF</span><br>
<span class=help1>%&nbsp;22-feb-2005,&nbsp;VF</span><br>
<br>
<hr>
start_time&nbsp;=&nbsp;cputime;<br>
[dim,num_data]=size(X);<br>
<br>
<span class=comment>%&nbsp;process&nbsp;input&nbsp;arguments</span><br>
<span class=comment>%------------------------------------</span><br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options=c2s(options);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'ker'</span>),&nbsp;options.ker&nbsp;=&nbsp;<span class=quotes>'linear'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'arg'</span>),&nbsp;options.arg&nbsp;=&nbsp;1;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'m'</span>),&nbsp;options.m&nbsp;=&nbsp;fix(0.25*num_data);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'p'</span>),&nbsp;options.p&nbsp;=&nbsp;options.m;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'maxerr'</span>),&nbsp;options.maxerr&nbsp;=&nbsp;1e-6;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'mserr'</span>),&nbsp;options.mserr&nbsp;=&nbsp;1e-6;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'verb'</span>),&nbsp;options.verb&nbsp;=&nbsp;0;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'lambda'</span>),&nbsp;options.lambda&nbsp;=&nbsp;0.001;&nbsp;<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;greedy&nbsp;algorithm&nbsp;to&nbsp;select&nbsp;subset&nbsp;of&nbsp;training&nbsp;data</span><br>
<span class=comment>%-------------------------------------------------------</span><br>
<br>
[inx,Alpha,Z,kercnt,MsErr,MaxErr]&nbsp;=&nbsp;...<br>
&nbsp;&nbsp;greedyappx(X,options.ker,options.arg,...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.m,options.p,options.mserr,options.maxerr,options.verb);&nbsp;<br>
&nbsp;&nbsp;<br>
<span class=comment>%&nbsp;apply&nbsp;ordinary&nbsp;linear&nbsp;least&nbsp;squares</span><br>
<span class=comment>%------------------------------</span><br>
w&nbsp;=&nbsp;inv(&nbsp;Z*Z'&nbsp;+&nbsp;options.lambda*num_data*eye(size(Z,1)))&nbsp;*&nbsp;Z*y;<br>
<br>
<span class=comment>%&nbsp;fill&nbsp;up&nbsp;the&nbsp;output&nbsp;model</span><br>
<span class=comment>%-------------------------------------</span><br>
model.Alpha&nbsp;=&nbsp;Alpha'*w;<br>
model.nsv&nbsp;=&nbsp;length(Alpha);&nbsp;&nbsp;<br>
model.b&nbsp;=&nbsp;0;<br>
model.sv.X=&nbsp;X(:,inx);<br>
model.sv.inx&nbsp;=&nbsp;inx;<br>
model.kercnt&nbsp;=&nbsp;kercnt;<br>
model.GreedyMaxErr&nbsp;=&nbsp;MaxErr;<br>
model.GreedyMsErr&nbsp;=&nbsp;MsErr;<br>
model.options&nbsp;=&nbsp;options;<br>
model.cputime&nbsp;=&nbsp;cputime&nbsp;-&nbsp;start_time;<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'kernelproj'</span>;<br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%&nbsp;EOF</span><br>
</code>
